package newspackage;

import java.util.ArrayList;
import java.util.List;

import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonParser;

/**
 * @author www.GetAllPic.com
 * 
 */
public class APIParser {

	public static ArrayList<NewsBean> getNews(String jsonArrayString)
			throws JsonParseException {
		// Log.d(TAG, "OnSucess "+jsonArrayString);
		ArrayList<NewsBean> newsList = new ArrayList<NewsBean>();
		try {
			JsonParser jp = new JsonParser();
			JsonElement root = jp.parse(jsonArrayString);
			JsonArray jsonArray = root.getAsJsonArray();
			int len = jsonArray.size();
			Gson gson = new Gson();

			for (int i = 0; i < len; i++) {
				NewsBean news = gson.fromJson(jsonArray.get(i), NewsBean.class);

				newsList.add(news);
			}
		} catch (Exception excep) {
			throw new JsonParseException("Pasring Error");
		}
		return newsList;
	}

	public static List<AgendaBean> getAgendas(String jsonArrayString)
			throws JsonParseException {
		// Log.d(TAG, "OnSucess "+jsonArrayString);
		ArrayList<AgendaBean> eventList = new ArrayList<AgendaBean>();
		try {
			JsonParser jp = new JsonParser();
			
			JsonElement root = jp.parse(jsonArrayString);
			JsonArray jsonArray = root.getAsJsonArray();
			int len = jsonArray.size();
			Gson gson = new Gson();

			for (int i = 0; i < len; i++) {
				JsonElement agendaElement = jsonArray.get(i).getAsJsonObject().get("event");
				AgendaBean agenda = gson.fromJson(agendaElement, AgendaBean.class);
				eventList.add(agenda);
			}
		} catch (Exception excep) {
			Log.w("json exception", excep.getMessage());
			return new ArrayList<AgendaBean>();
		}
		return eventList;
	}
	
}
